#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Zabbix Tool - 主入口程序
"""

import sys
import os

# 确保 PyQt5 是第一个导入的包，避免打包后找不到问题
try:
    from PyQt5.QtWidgets import QApplication
    from PyQt5.QtCore import QLibraryInfo
except ImportError:
    print("找不到 PyQt5 模块，请确保已安装: pip install PyQt5==5.15.6")
    sys.exit(1)

from gui.main_window import MainWindow
from src.config import load_config

def set_qt_plugin_path():
    """设置 Qt 插件路径"""
    if hasattr(sys, '_MEIPASS'):
        base_path = sys._MEIPASS
        # 使用os.path.join处理路径
        qt_plugin_path = os.path.join(base_path, 'PyQt5', 'Qt', 'plugins')
        if not os.path.exists(qt_plugin_path):
            qt_plugin_path = os.path.join(base_path, 'plugins')
        os.environ['QT_PLUGIN_PATH'] = qt_plugin_path
    else:
        # 开发环境路径
        os.environ['QT_PLUGIN_PATH'] = QLibraryInfo.location(QLibraryInfo.PluginsPath)

def main():
    """主程序入口点"""
    # 设置 Qt 插件路径
    set_qt_plugin_path()
    
    # 加载配置
    config = load_config()
    
    # 启动GUI应用
    app = QApplication(sys.argv)
    window = MainWindow(config)
    window.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()